<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/14 0014
 * Time: 上午 11:11
 */
namespace app\common\validate;

class CouponValidate extends BaseValidate{
    protected $rule = [
        'coupon_type' => 'require|checkcoupon_type',
        'name' => 'require|isNotEmpty',
        'number' => 'require|checknumber',
        'coupon_purpose' => 'require|checkcoupon_purpose',
        'start_time' => 'require|isNotEmpty',
        'end_time' => 'require|isNotEmpty',
        'cash_amount' => 'checkcash_amount'
    ];

    protected $message = [
        'coupon_type' => '优惠券类型错误',
        'name' => '优惠券名称必填',
        'number' => '优惠券数量有误',
        'coupon_purpose' => '领取途径数据错误',
        'start_time' => '开始时间必填',
        'end_time' => '结束时间必填',
        'cash_amount' => '抵现金额不合法'
    ];

    protected function checkcoupon_type($value){
        if(in_array($value,[1,2,3,4,5,6])){
            return true;
        }
        return false;
    }
    protected function checknumber($value){
        if(preg_match("/^[1-9][0-9]*$/",$value)){
            return true;
        }
        return false;
    }

    protected function checkcoupon_purpose($value){
        if(in_array($value,[1,2,3])){
            return true;
        }
        return false;
    }

    protected function checkcash_amount($value){
        $match = "/(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/";
        if($value < 0){
            return false;
        }
        if(preg_match($match,$value)){
            return true;
        }
        return false;
    }
}